package com.bobo.netty.server.telnet;

import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.springframework.util.StringUtils;

public class NettyTelnetHandler extends SimpleChannelInboundHandler<String> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        String response;
        Boolean close = false;
        if(StringUtils.isEmpty(msg)){
            response = "please input something";
        }else if(msg.equals("bye")){
            response = "Have a good day!\r\n";
            close = true;
        }else{
            response = "Did you say '" + msg + "'?\r\n";
        }
        ChannelFuture future = ctx.write(response);
        ctx.flush();
        if(close){
            future.addListener(ChannelFutureListener.CLOSE);
        }

    }
}
